$(function () {
  const form = layui.form
  const layer = layui.layer
  form.verify({
    nickname: val => {
      if (val.length > 6) return '昵称长度必须在 1 ~ 6 个字符之间！'
    },
    email: [/@/, '请输入正确的邮箱地址'],
  })
  const initUserinfo = () => {
    $.ajax({
      type: 'GET',
      url: '/my/userinfo',
      success: res => {
        const { status, message, data } = res
        if (status !== 0) return layer.msg(message)
        // console.log(data)
        form.val('formUserInfo', data)
      },
    })
  }

  $('#btnReset').click(function (e) {
    e.preventDefault()
    initUserinfo()
  })

  $('.layui-form').submit(function (e) {
    e.preventDefault()
    const data = $(this).serialize()
    $.ajax({
      type: 'POST',
      url: '/my/userinfo',
      data,
      success: res => {
        const { status, message } = res
        layer.msg(message)
        if (status !== 0) return
        window.parent.getUserInfo()
      },
    })
  })

  initUserinfo()
})
